<?php
	

defined('_JEXEC') or die();
jimport('joomla.application.component.model');

class NBEventMapsModelEditLieux extends JModel
{
	var $_list;
	var $_id;
	var $_data;

	function __construct()
	{
		parent::__construct();
		$doc =& JFactory::getDocument();

		$array = JRequest::getVar('cid',  0, '', 'array');
		$this->setId((int)$array[0]);
		
		
		if (!defined( '_JOS_NBEVENTMAP_CSS' ))
		{
			
			define( '_JOS_NBEVENTMAP_CSS', 1 );
	
			$doc->addCustomTag( '<link rel="stylesheet" href="'.JURI::base().'components/com_nbeventmap/views/nbeventmap/nbeventmap.css" type="text/css" />'); 
			$doc->addCustomTag( '<link rel="stylesheet" href="'.JURI::base().'components/com_nbeventmap/views/nbeventmap/nbeventmap2.css" type="text/css" />'); 
		}
	}

	function _buildQuery()
	{
		$query = ' SELECT * '
			. ' FROM #__nbeventmap_marqueurs where published = 1 '
		;

		return $query;
	}

	function setId($id)
	{
		
		$this->_id		= $id;
		$this->_data	= null;
	}


	function &getData()
	{
		
		if (empty( $this->_data )) {
			$query = ' SELECT * FROM #__nbeventmap '.
					'  WHERE id = '.$this->_id;
			$this->_db->setQuery( $query );
			$this->_data = $this->_db->loadObject();
		}
		if (!$this->_data) {
			$this->_data = new stdClass();
			$this->_data->id = 0;
			$this->_data->nom = null;
			$this->_data->alias = null;
			$this->_data->adresse = null;
			$this->_data->adresse2 = null;
			$this->_data->codepostal = null;
			$this->_data->ville = null;
			$this->_data->departement = null;
			$this->_data->pay = null;
			$this->_data->tel = null;
			$this->_data->tel2 = null;
			$this->_data->fax = null;
			$this->_data->email = null;
			$this->_data->web = null;
			$this->_data->img = null;
			$this->_data->album = null;
			$this->_data->intro = null;
			$this->_data->message = null;
			$this->_data->horaires_prix = null;
			$this->_data->affichage = null;
			$this->_data->marqueur = null;
			$this->_data->link = null;
			$this->_data->icon = null;
			$this->_data->icon_label = null;
			$this->_data->glat = null;
			$this->_data->glng = null;
			$this->_data->gzoom = null;
			$this->_data->catid = null;
			$this->_data->userid = null;
			$this->_data->published = null;
			$this->_data->checked_out = null;
			$this->_data->metadesc = null;
			$this->_data->metakey = null;
			$this->_data->ordering = 0;			
		}
		
		if (JString::strlen($this->_data->message) > 1) {
			$this->_data->text = $this->_data->intro . "<hr id=\"system-readmore\" />" . $this->_data->message;
		} else {
			$this->_data->text = $this->_data->intro;
		}
		return $this->_data;
	}

	function &getMarqueurs()
	{
		if (empty( $this->_list ))
		{
			$query = $this->_buildQuery();
			$this->_list = $this->_getList( $query );
		}

		return $this->_list;
	}

	

	function &getUser($id)
	{
		$query = ' (SELECT userid '
			. ' FROM #__nbeventmap '
			. ' WHERE id='.$id.')'
		;
		$query = ' SELECT name, email '
			. ' FROM #__users '
			. ' WHERE id='.$query
		;
        $this->_db->setQuery( $query );
        $user = $this->_db->loadObject();

		return $user;
	}

	function store($data)
	{
		$row =& $this->getTable('nbeventmap');

		


		
		$this->saveNBEventmapPrep( $row );


		
		if (!$row->bind($data)) {
			$this->setError($this->_db->getErrorMsg());
			return false;
		}

		
		if (!$row->check()) {
			$this->setError($this->_db->getErrorMsg());
			return false;
		}
		
		
		if (!$row->store()) {
			$this->setError( $this->_db->getErrorMsg() );
			return false;
		}
		
		
		if (!$row->reorder()) {
			$this->setError( $this->_db->getErrorMsg() );
			return false;
		}
		/* upload files */
			$pictures = $_FILES['files'];
			$gallery = new stdClass();

			$array_arr = array();
			for($i=0;$i< count($pictures['type']);$i++) {
				//var_dump($image_type);die;
				if (($pictures['type'][$i] == "image/gif")
				|| ($pictures['type'][$i] == "image/jpeg")
				|| ($pictures['type'][$i] == "image/png")
				|| ($pictures['type'][$i] == "image/jpg"))
				{
					$array_arr['type'][]= $pictures['type'][$i];
					$array_arr['name'][]= $pictures['name'][$i];
					$array_arr['tmp_name'][]= $pictures['tmp_name'][$i];
				}
			}
			foreach ($array_arr['name'] as $k=>$image) {
				$image_name = time() . $image;
		  		$paths = JPATH_SITE . DS . 'media' . DS . 'netbase' . DS . 'images'. DS . $image_name;
		        move_uploaded_file($array_arr['tmp_name'][$k], $paths);
		        $gallery->event_id = $row->id;
        		$gallery->name = $image_name;	

        		$gallery->id = 0;/*auto increment*/
				if (!$this->_db->insertObject( '#__nbeventmap_files', $gallery, 'id' )) {
	   		 		echo $database->stderr();
	    			return false;
	 			}
			}
		
		return $row->id;
	}
	//get files
	function getFiles()
	{
		$db =& JFactory::getDBO();
		$cid = JRequest::getVar('cid');
		if(is_array($cid)) $cid = $cid[0];
		$db->setQuery("SELECT * FROM #__nbeventmap_files WHERE event_id = '".$cid."'");
		$rows = $db->loadObjectList();
		return $rows;
	}

	function getEventId($id)
	{
		$db =& JFactory::getDBO();
		$sql = "SELECT DISTINCT(event_id) FROM #__nbeventmap_files WHERE id = ".$id;
		$db->setQuery($sql);
		$rows = $db->loadObject();
		return $rows;
	}
	function deleteFiles($id)
	{
		$db =& JFactory::getDBO();
		$sql = "DELETE FROM #__nbeventmap_files WHERE id = ".$id;
		$db->setQuery($sql);
		$db->query();
	}

	function saveNBEventMapPrep( &$row )
	{
		
		$text_horaires_prix = JRequest::getVar( 'text_horaires_prix', '', 'post', 'string', JREQUEST_ALLOWRAW );
		$text_message		= JRequest::getVar( 'text_message', '', 'post', 'string', JREQUEST_ALLOWRAW );
		$text_link			= JRequest::getVar( 'text_link', '', 'post', 'string', JREQUEST_ALLOWRAW );

		
		$text_horaires_prix		= str_replace( '<br>', '<br />', $text_horaires_prix );
		$text_message		= str_replace( '<br>', '<br />', $text_message );
		$text_link		= str_replace( '\\', '/', $text_link );

		
		$pattern = '#<hr\s+id=("|\')system-readmore("|\')\s*\/*>#i';
		$tagPos	= preg_match($pattern, $text_message);

		if ( $tagPos == 0 )
		{
			$row->intro	= $text_message;
		} else
		{
			list($row->intro, $row->message) = preg_split($pattern, $text_message, 2);
		}

		$row->horaires_prix	= $text_horaires_prix;
		$row->link	= $text_link;

		
		if (!$row->userid) {
			$user		= & JFactory::getUser();
			$row->userid = $user->get('id');
		}


		return true;
	}
	
	function sendmail($id)
	{
		$mainframe = &JFactory::getApplication(); 

		$user = $this->getUser($id);
		$config = &$mainframe->getParams();
	    
		$disable_https = true;

		if (!$disable_https) {
			$url = (!empty($_SERVER['HTTPS'])) ? "https://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'] : "http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
		} else {
			$url = "http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
		}
		$mySubject = JText::_('NBEVENTMAP_NEW_SUBMIT');
		$myMessage = JText::_('NBEVENTMAP_NEW_SUBMIT2')." \n";
		$myMessage.= 'http://'.$_SERVER['SERVER_NAME']."/administrator/index.php?option=com_nbeventmap&controller=nbeventmap&task=edit&cid=".$id."\n\n";
		$myMessage.= JText::_('NBEVENTMAP_BY_USER')." ";
		$myMessage.= $user->name;
		$myMessage.= "\n\n".JText::_('NBEVENTMAP_MERCI_PUBLIER');
		$myRecipient = $config->get('moderateur');
		
		$mailSender = &JFactory::getMailer();
		$mailSender->addRecipient($myRecipient);
		$mailSender->setSender(array('mon site','submit NBEventMap'));
		$mailSender->addReplyTo(array( $user->email, '' ));
		$mailSender->setSubject($mySubject);
		$mailSender->setBody($myMessage);
		if (!$mailSender->Send()) {
			return false;
		} else {
			return true;
		}
	}

}
?>
